package com.balance.anole.erp.component;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

public class TextEditingSupport extends AbstractEditingSupport {

	private UpdateValueStrategy targetToModelUVS;

	private DataBindingContext dbc;

	public TextEditingSupport(ColumnViewer viewer, DataBindingContext dbc, String propertyName) {
		super(viewer, dbc);
		this.cellEditor =  new TextCellEditor((Composite) viewer.getControl());
		this.propertyName = propertyName;
		this.dbc = dbc;
	}

	public TextEditingSupport(ColumnViewer viewer, DataBindingContext dbc, String propertyName, UpdateValueStrategy targetToModelUVS) {
		this(viewer, dbc, propertyName);
		this.targetToModelUVS = targetToModelUVS;
	}

	@Override
	protected IObservableValue doCreateCellEditorObservable(
			CellEditor cellEditor) {
		return SWTObservables.observeText(cellEditor.getControl(),
				SWT.Modify);
	}

	protected Binding createBinding(IObservableValue target,
			IObservableValue model) {
		if(targetToModelUVS != null){
			return dbc.bindValue(target, model, targetToModelUVS, null);
		}else{
			return super.createBinding(target, model);
		}
	}

	public Control getControl(){
		return this.cellEditor.getControl();
	}
}
